package com.handu.luna.core.domain.admin;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Transient;

import com.handu.luna.core.domain.BasePersistentDomain;

@Entity
public class AdminControllerDescription extends BasePersistentDomain<AdminControllerDescriptionRepository> {
	
	public static final String DELIMITER = ",";
	
	private String name;
	private String description;
	private String actions;
	private String actionNames;
	private String actionDescriptions;
	private long categoryId;
	
	@Transient private AdminControllerCategory category;
	@Transient private List<String> actionList;
	@Transient private List<String> actionNameList;
	@Transient private List<String> actionDescriptionList;
	
	public AdminControllerDescription() {
	}	
	public AdminControllerDescription(String name, String description, String actions,
			String actionNames, String actionDescriptions) {
		super();
		this.name = name;
		this.description = description;
		this.actions = actions;
		this.actionNames = actionNames;
		this.actionDescriptions = actionDescriptions;
	}
	public AdminControllerDescription(String name, String description, long categoryId) {
		super();
		this.name = name;
		this.description = description;
		this.categoryId = categoryId;
		this.actions = "index,list,add,edit,save,delete,update";
		this.actionNames = description + "管理,"+description+"管理,"+description+"添加,"+description+"编辑,"+description+"保存,"+description+"删除"+description+"更新";
		this.actionDescriptions = actionNames;
	}
	
	public static AdminControllerDescription createErrorDescription(){
		AdminControllerDescription d = new AdminControllerDescription("错误页面","错误页面",0);
		d.setCategory(new AdminControllerCategory("错误页面"));
		d.init();
		return d;
	}

	@javax.persistence.PostLoad
	public void init(){
		actionList = toList(actions);
		actionNameList = toList(actionNames);
		actionDescriptionList = toList(actionDescriptions);
	}
	
	private List<String> toList(String arrayString){
		ArrayList<String> list = new ArrayList<String>();
		for(String item:arrayString.split(DELIMITER))list.add(item);
		return list;
	}
	
	public String getActionName(String action){
		return this.actionNameList.get(this.actionList.indexOf(action));
	}
	public String getActionDescription(String action){
		return this.actionDescriptionList.get(this.actionList.indexOf(action));
	}	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getActions() {
		return actions;
	}
	public void setActions(String actions) {
		this.actions = actions;
	}
	public String getActionNames() {
		return actionNames;
	}
	public void setActionNames(String actionNames) {
		this.actionNames = actionNames;
	}
	public String getActionDescriptions() {
		return actionDescriptions;
	}
	public void setActionDescriptions(String actionDescriptions) {
		this.actionDescriptions = actionDescriptions;
	}
	public List<String> getActionList() {
		return actionList;
	}
	public void setActionList(List<String> actionList) {
		this.actionList = actionList;
	}
	public List<String> getActionNameList() {
		return actionNameList;
	}
	public void setActionNameList(List<String> actionNameList) {
		this.actionNameList = actionNameList;
	}
	public List<String> getActionDescriptionList() {
		return actionDescriptionList;
	}
	public void setActionDescriptionList(List<String> actionDescriptionList) {
		this.actionDescriptionList = actionDescriptionList;
	}
	public long getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(long categoryId) {
		this.categoryId = categoryId;
	}
	public AdminControllerCategory getCategory() {
		return category;
	}
	public void setCategory(AdminControllerCategory category) {
		this.category = category;
	}
	
}
